<?php

require_once("database_api.php");
require_once("defines_inc.php");

require_once("image_upload_api.php");
require_once("utilites_inc.php");

class share_api
{
	private $db;
	
	public function __construct($db) 
	{
		$this->db = $db;
    }
	
	private function create_project($name, $desc, $user_id, $type, $category_id)
	{
		$transform_desc = htmlEscapeAndLinkUrls($desc);
		
		$sql = $this->db->safeQuery("INSERT INTO projects (PRJ_NAME, PRJ_DESC, PRJ_DATE, PRJ_TYPE, PRJ_USR_ID, PRJ_CAT_ID) VALUES('{0}', '$transform_desc', now(), {1}, {2}, {3})", 
			$name, $type, $user_id, $category_id);
		
		if($this->db->query($sql) === TRUE)
		{
			return $this->db->getLastID();
		}
	}		
	
	
	public function create_video_project($name, $desc, $user_id, $url, $category_id)
	{
		$video_type = 0;
		$video_id = 0;
		
		if(preg_match("/^[a-zA-Z]+:\/\/(www\.)?vimeo\.com\/(\d+).*$/", $url, $matches))
		{
			$video_type = VIDEO_VIMEO_TYPE;
			$video_id = $matches[2];
		}
		else if(preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+(?=\?)|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\n]+#", $url, $matches))
		{
			$video_type = VIDEO_YOUTUBE_TYPE;
			$video_id = $matches[0];
		}
		else
		{
			return 0;
		}
		
		
		$this->db->beginTransaction();
		
		$project_id = $this->create_project($name, $desc, $user_id, PROJECT_TYPE_VIDEO, $category_id);
					
		$sql = $this->db->safeQuery("INSERT INTO project_video (PRV_PRJ_ID, PRV_TYPE, PRV_VIDEO) VALUES({0}, {1}, '{2}')", $project_id, $video_type, $video_id);
		$this->db->query($sql);
		
		$this->db->commitTransaction();
				
		return $project_id;
		
		
	}
	
	public function create_image_project($name, $desc, $user_id, $file, $category_id)
	{
		$upload = new image_upload_api();
		
		$upload->upload_dir = GALLERY_TEMP_PATH;
		$upload->foto_folder = GALLERY_PHOTO_PATH;			
		$upload->thumb_folder = GALLERY_THUMB_PATH;
		$upload->extensions = array('.png', '.jpg', '.bmp', '.jpeg');
		$upload->language = "en";
		
		
		$upload->x_max_size = IMAGE_MAX_SIZE;
		$upload->y_max_size = IMAGE_MAX_SIZE;
		$upload->x_max_thumb_size = THUMB_MAX_SIZE;
		$upload->y_max_thumb_size = THUMB_MAX_SIZE;
		
		$upload->rename_file = true;
		
		$upload->the_temp_file = $file['tmp_name'];
		$upload->the_file = $file['name'];
		$upload->http_error = $file['error'];
		$upload->do_filename_check = 'y';
		
		
		if ($upload->upload())
		{
			if($upload->process_image(true, true))
			{
				$this->db->beginTransaction();
		
				$project_id = $this->create_project($name, $desc, $user_id, PROJECT_TYPE_IMAGE, $category_id);
		
				$sql = $this->db->paramQuery("INSERT INTO project_image (PRI_PRJ_ID, PRI_FILENAME) VALUES({0}, '{1}')", $project_id, $upload->file_copy);
				$this->db->query($sql);
		
				$this->db->commitTransaction();
				
				return $project_id;
			}
		}			
		
		return 0;
	}
	
	public function get_category_list()
	{
		$sql = "SELECT CAT_ID, CAT_NAME FROM categories ORDER BY CAT_NAME ASC";
		return $this->db->query($sql);
	}
	
	public function get_share_list()
	{
		$sql = $this->db->paramQuery("SELECT PRJ_ID, USR_NAME, USR_ID, PRJ_TYPE, PRJ_DESC, PRJ_NAME, PRI_FILENAME, PRV_TYPE, PRV_VIDEO,"
		. " CAT_NAME, LIKEIT_COUNT FROM projects"
		. " LEFT JOIN project_image ON PRI_PRJ_ID = PRJ_ID"
		. " LEFT JOIN project_video ON PRV_PRJ_ID = PRJ_ID"
		. " INNER JOIN categories ON CAT_ID = PRJ_CAT_ID"
		. " LEFT JOIN (SELECT COUNT(LIT_ID) AS LIKEIT_COUNT, LIT_PRJ_ID FROM likeit GROUP BY LIT_PRJ_ID) AS LKT ON PRJ_ID = LIT_PRJ_ID"
		. " INNER JOIN users ON USR_ID = PRJ_USR_ID"
		. " ORDER BY PRJ_DATE DESC");
		
		return $this->db->query($sql);
	}
	
	public function get_share_feed_list($limit)
	{
		$sql = $this->db->paramQuery("SELECT PRJ_ID, USR_NAME, USR_ID, PRJ_TYPE, PRJ_DESC, PRJ_NAME, PRI_FILENAME, PRV_TYPE, PRV_VIDEO,"
		. " CAT_NAME, LIKEIT_COUNT FROM projects"
		. " LEFT JOIN project_image ON PRI_PRJ_ID = PRJ_ID"
		. " LEFT JOIN project_video ON PRV_PRJ_ID = PRJ_ID"
		. " INNER JOIN categories ON CAT_ID = PRJ_CAT_ID"
		. " LEFT JOIN (SELECT COUNT(LIT_ID) AS LIKEIT_COUNT, LIT_PRJ_ID FROM likeit GROUP BY LIT_PRJ_ID) AS LKT ON PRJ_ID = LIT_PRJ_ID"
		. " INNER JOIN users ON USR_ID = PRJ_USR_ID"
		. " ORDER BY PRJ_DATE DESC LIMIT {0}", $limit);
		
		return $this->db->query($sql);
	}
	
	public function get_subscribers_feed_list($user_id, $limit)
	{
		$sql = $this->db->paramQuery("SELECT PRJ_ID, USR_NAME, USR_ID, PRJ_TYPE, PRJ_DESC, PRJ_NAME, PRI_FILENAME, PRV_TYPE, PRV_VIDEO,"
		. " CAT_NAME, LIKEIT_COUNT FROM projects"
		. " LEFT JOIN project_image ON PRI_PRJ_ID = PRJ_ID"
		. " LEFT JOIN project_video ON PRV_PRJ_ID = PRJ_ID"
		. " INNER JOIN categories ON CAT_ID = PRJ_CAT_ID"
		. " LEFT JOIN (SELECT COUNT(LIT_ID) AS LIKEIT_COUNT, LIT_PRJ_ID FROM likeit GROUP BY LIT_PRJ_ID) AS LKT ON PRJ_ID = LIT_PRJ_ID"
		. " INNER JOIN users ON USR_ID = PRJ_USR_ID"
		. " INNER JOIN subscribers ON SBC_USR_ID = {1} AND SBC_SUB_USR_ID = USR_ID"		
		. " ORDER BY PRJ_DATE DESC LIMIT {0}", $limit, $user_id);
		
		return $this->db->query($sql);
	}
	
	public function get_share_user_list($user_id)
	{
		$sql = $this->db->paramQuery("SELECT PRJ_ID, USR_NAME, USR_ID, PRJ_TYPE, PRJ_DESC, PRJ_NAME, PRI_FILENAME, PRV_TYPE, PRV_VIDEO,"
		. " CAT_NAME, LIKEIT_COUNT FROM projects"
		. " LEFT JOIN project_image ON PRI_PRJ_ID = PRJ_ID"
		. " LEFT JOIN project_video ON PRV_PRJ_ID = PRJ_ID"
		. " INNER JOIN categories ON CAT_ID = PRJ_CAT_ID"
		. " INNER JOIN users ON USR_ID = PRJ_USR_ID"
		. " LEFT JOIN (SELECT COUNT(LIT_ID) AS LIKEIT_COUNT, LIT_PRJ_ID FROM likeit GROUP BY LIT_PRJ_ID) AS LKT ON PRJ_ID = LIT_PRJ_ID"
		. " WHERE USR_ID = {0}"
		. " ORDER BY PRJ_DATE DESC", $user_id);
		
		return $this->db->query($sql);
	}
	
	public function get_project($project_id)
	{
		$sql = $this->db->safeQuery("SELECT PRJ_NAME, PRJ_TYPE, PRJ_DESC, PRV_TYPE, PRV_VIDEO, USR_NAME, PRI_FILENAME, USR_ID, PRJ_DATE,"
		. " CAT_NAME FROM projects"
		. " LEFT JOIN project_video ON PRV_PRJ_ID = PRJ_ID"
		. " LEFT JOIN project_image ON PRI_PRJ_ID = PRJ_ID"
		. " INNER JOIN categories ON CAT_ID = PRJ_CAT_ID"
		. " INNER JOIN users ON USR_ID = PRJ_USR_ID"
		. " WHERE PRJ_ID = {0} LIMIT 1", $project_id);
		
		if($result = $this->db->query($sql))
		{
			$row = $result->fetch_assoc();
			$result->free();
			
			return $row;
		}
	}
	
	public function toggle_project_likteit($project_id, $user_id)
	{
		$sql = $this->db->safeQuery("SELECT LIT_ID FROM likeit WHERE LIT_USR_ID = {0} AND LIT_PRJ_ID = {1} LIMIT 1", $user_id, $project_id);
		
		if($result = $this->db->query($sql))
		{
			$row = $result->fetch_assoc();
			$result->free();
			
			if($row)
			{
				$sql = $this->db->safeQuery("DELETE FROM likeit WHERE LIT_USR_ID = {0} AND LIT_PRJ_ID = {1}", $user_id, $project_id);
				$this->db->query($sql);
			}
			else
			{
				$sql = $this->db->safeQuery("INSERT INTO likeit (LIT_USR_ID, LIT_PRJ_ID) VALUES({0}, {1})", $user_id, $project_id);
				$this->db->query($sql);
			}
					
		}
	}
	
	public function get_count_likeit($project_id)
	{
		$sql = $this->db->safeQuery("SELECT COUNT(LIT_ID) AS LIKEIT_COUNT FROM likeit WHERE LIT_PRJ_ID = {0}", $project_id);
		
		if($result = $this->db->query($sql))
		{
			$row = $result->fetch_assoc();
			$result->free();
			
			return $row["LIKEIT_COUNT"];
		}
		
		return 0;
	}
}

?>